<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title></title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="../../../lib/layui-v2.6.3/css/layui.css" media="all">
  <link rel="stylesheet" href="../../../css/public.css" media="all">
  <style>
      body {
          background-color: #ffffff;
      }
  </style>
</head>
<body>
<form class="layui-form" action="" lay-filter="demo-val-filter">
  <div class="layui-form layuimini-form" id="myForm">
    <div class="layui-form-item">
      <label class="layui-form-label required">名称</label>
      <div class="layui-input-block">
        <input type="text" name="roleName" lay-verify="required" placeholder="请输入" value="" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label required">角色代码</label>
      <div class="layui-input-block">
        <input type="text" name="roleCode" lay-verify="required" lay-reqtext="不能为空" placeholder="请输入" value=""
               class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label required">角色类型</label>
      <div class="layui-input-block">
        <select id="roleType" lay-verify="required" name="roleType">
          <option value="0">超级管理员</option>
          <option value="1">系统管理员</option>
          <option value="2">其他角色</option>
        </select>
      </div>
    </div>

    <div class="layui-form-item">
      <div class="layui-input-block">
        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn" type="button">保存</button>
      </div>
    </div>
  </div>
</form>

<script src="../../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../../lib/jsencrypt/jsencrypt.js" charset="utf-8"></script>
<script src="../../../api/common.js" charset="utf-8"></script>
<script>
  layui.use(['form'], function () {
    var form = layui.form,
      layer = layui.layer,
      $ = layui.$;

    function getQueryString(name) {
      var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
      var r = window.location.search.substr(1).match(reg);
      if (r != null) {
        return decodeURIComponent(r[2]);
      }
      return null;
    }

    var id = getQueryString('id');

    $.ajax({
      type: 'GET',
      url: local + 'role/' + id,
      dataType: "json",
      headers: {
        'Authorization': `Bearer ${ localStorage.getItem("token") }`,
      },
      contentType: 'application/json;charset=UTF-8',
      success: function (res) {
        form.val('demo-val-filter', {
          'roleName':res.data.roleName,
          'roleCode':res.data.roleCode,
          'roleType':res.data.roleType,
        });
        form.render('select')
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, {icon: 5});
      }
    });

    //监听提交
    form.on('submit(saveBtn)', function (data) {
      var obj = data.field

      $.ajax({
        type: 'POST',
        url: local + 'role/insert',
        data: JSON.stringify(obj),
        dataType: "json",
        headers: {
          'Authorization': `Bearer ${ localStorage.getItem("token") }`,
        },
        contentType: 'application/json;charset=UTF-8',
        success: function (res) {//res为相应体,function为回调函数
          console.log('data', res)
          if (res.code === 200) {
            // layer.alert('添加成功');
            var iframeIndex = parent.layer.getFrameIndex(window.name);
            parent.layer.close(iframeIndex);
            parent.layui.table.reload('currentTableId');
            return false;
          }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, {icon: 5});
        }
      });
    });
  });
</script>
</body>
</html>
